<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleInit"> 初始化</a-button>
        <a-button type="primary" @click="handleId"> 同步主键</a-button>
        <a-button type="primary" @click="handleCode"> 代码生成</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls" disabled> 导出</a-button>
        <!-- <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls" disabled>导入</j-upload-button> -->
      </template>
      <template #status="{ record }">
        <Tag v-if="record.status == 0" color="red">未同步</Tag>
        <Tag v-if="record.status == 1" color="green">已同步</Tag>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts" name="online-genDataTable" setup>
import { reactive } from 'vue';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage'
import { columns, searchFormSchema } from './GenDataTableData';
import { list, deleteOne, getImportUrl, getExportUrl, init, updateGenColumn, updateTableId } from './GenDataTableApi';
import { Tag, Modal, Input } from 'ant-design-vue';
import { useRouter } from 'vue-router'; // 路由跳转
const router = useRouter();
import { useMessage } from '/@/hooks/web/useMessage'; // 轻提醒
const { createMessage: $message, createConfirm } = useMessage(); // 解构

const queryParam = reactive<any>({});
//注册table数据
const { tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '系统数据库表',
    api: list,
    columns,
    canResize: false,
    showIndexColumn: true,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [],
      fieldMapToTime: [],
    },
    actionColumn: {
      width: 240,
      fixed: 'right'
    },
    beforeFetch: (params) => {
      // params.pageSize = 20;
      return Object.assign(params, queryParam);
    },
  },
  exportConfig: {
    name: "系统数据库表",
    url: getExportUrl,
    params: queryParam,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
})

const [registerTable, { reload }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;

/**
 * 初始化事件
 */
async function handleInit() {
  Modal.confirm({
    title: '初始化',
    content: '初始化会删除已配置的数据，并重新生成，是否继续',
    okText: '确认',
    cancelText: '取消',
    onOk: async () => {
      await init({});
      handleSuccess();
    },
  });
}

/**
 * 代码生成
 */
async function handleCode() {
  // $message.info('代码生成');
  router.push({ name: `/zhc/db/gen`, query: { tableName: '1234' } });
}

/**
 * id生成
 */
async function handleId() {
  let length = selectedRowKeys.value.length;
  if (length === 0) {
    $message.error('请先选择一条数据!!');
    return;
  } else if (length > 1) {
    $message.error('不能选择多条数据!!');
    return;
  }
  await updateTableId(selectedRowKeys.value[0]);
}

/**
 * 同步数据库表字段事件
 */
async function handleGenColumn(record: Recordable) {
  // createConfirm({
  //   iconType: 'warning',
  //   title: '确认删除',
  //   content: '是否删除选中数据',
  //   okText: '确认',
  //   cancelText: '取消',
  //   onOk: async () => {
  //     await updateGenColumn(record.id);
  //     handleSuccess();
  //   }
  // });
  if (record.status == 1) {
    Modal.confirm({
      title: '同步',
      content: '当前表已同步，是否重新同步？',
      okText: '确认',
      cancelText: '取消',
      onOk: async () => {
        await updateGenColumn(record.id);
        handleSuccess();
      },
    });
  } else {
    await updateGenColumn(record.id);
    handleSuccess();
  }
}

/**
 * 同步数据库表字段事件
 */
async function handleViewField(record: Recordable) {
  router.push({ name: `/zhc/db/GenDataTableColumn`, query: { tableName: record.tableName } });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
 * 表格操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '同步',
      // icon: 'clarity:note-edit-line',
      onClick: handleGenColumn.bind(null, record)
    },
    {
      label: '查看字段信息',
      onClick: handleViewField.bind(null, record)
    },
    {
      label: '删除',
      color: 'error',
      popConfirm: {
        title: '是否确认删除',
        placement: 'left',
        confirm: handleDelete.bind(null, record),
      }
    }
  ]
}
</script>

<style lang="less" scoped>
:deep(.ant-picker),
:deep(.ant-input-number) {
  width: 100%;
}
</style>